package com.meilai.project.mapper.system;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.entity.system.Dictionary;
import com.meilai.project.vo.SelectOptionVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author 张驰
 * @date 2020/8/15 10:08
 */
@Mapper
public interface DictionaryMapper extends BaseMapper<Dictionary> {
    @Select("select id from dictionary where code=#{code}")
    Long getIdByCode(@Param("code") String code);

    @Select("select * from dictionary where pid=#{pid} and `status`=1 and isnull(deleted_at) order by sort,id")
    List<Dictionary> getListByPid(@Param("pid") Long pid);

    @Select("select * from dictionary where pid=#{pid} order by sort,id")
    List<Dictionary> getListByPidAll(@Param("pid") Long pid);

    @Select("select * from dictionary where rid=#{rid} and level=3 and `status`=1 and isnull(deleted_at) order by sort,id")
    List<Dictionary> getListByRid(@Param("rid") Long rid);

    @Select("select * from dictionary where pid IN " +
            "(select id from dictionary where pid=#{pid} and `status`=1 and isnull(deleted_at)) " +
            "and level=4 and `status`=1 and isnull(deleted_at) order by sort,id")
    List<Dictionary> getList4thBy2ndId(@Param("pid") Long pid);

    @Select("select * from dictionary where rid=#{rid} and level=3 order by sort,id")
    List<Dictionary> getListByRidAll(@Param("rid") Long rid);

    @Select("select * from dictionary where pid=#{pid} and isnull(deleted_at) order by sort")
    List<Dictionary> getListByPidNoDel(@Param("pid") Long pid);

    List<Dictionary> list1stAnd2nd(@Param("name") String name, @Param("status") Integer status);

    List<Dictionary> list3rdAll(Page<Dictionary> page, @Param("pid") Long pid, @Param("name") String name, @Param("status") Integer status);

    List<Dictionary> list4thAll(Page<Dictionary> page, @Param("pid") Long pid, @Param("name") String name, @Param("status") Integer status);

    List<Integer> listIds(@Param("name") String name, @Param("status") Integer status);

    @Insert("<script>" +
            "INSERT INTO `dictionary`(`id`, `pid`, `rid`, `level`, `name`, `code`, `memo`, `params`, `hidden`, `status`, `sort`, `deleted_at`, `created_at`, `updated_at`) VALUES  " +
            "<foreach item=\"item\" index=\"index\" collection=\"dicts\" open=\"\" separator=\",\" close=\"\"> " +
            "(#{item.id}, #{item.pid}, #{item.rid}, #{item.level}, #{item.name}, #{item.code},  " +
            "#{item.memo}, #{item.params}, #{item.hidden}, #{item.status}, #{item.sort},  " +
            "#{item.deleted_at}, #{item.created_at}, #{item.updated_at})   " +
            "</foreach>" +
            "</script>")
    boolean saveSetPointTypes(@Param("dicts") List<Dictionary> dicts);

    List<SelectOptionVO> pointTypeSelectOptions();
}
